﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QSHC.LibrarySL.MVVM;
using QSHC.QSchedule.Web.Models;
using QSHC.QSchedule.Web.Services;
using System.Linq;

namespace QSHC.QSchedule.Silverlight.ViewModels
{
    public class AppointmentCardViewModel : ViewModelBase
    {
        QSHCDomainContext _dc = new QSHCDomainContext();
        #region Properties

        #region EmployeeName
        private string _employeeName;
        public string EmployeeName
        {
            get { return _employeeName; }
            set
            {
                if (_employeeName != value)
                {
                    _employeeName = value;
                    OnPropertyChanged(() => EmployeeName);
                }
            }
        }
        #endregion

        #region DivisionName    
        private string _divisionName;
        public string DivisionName
        {
            get { return _divisionName; }
            set
            {
                if (_divisionName != value)
                {
                    _divisionName = value;
                    OnPropertyChanged(() => DivisionName);
                }
            }
        }
        #endregion
        
        #region Property Appointment
        private Appointment _appointment;
        public Appointment Appointment
        {
            get { return _appointment; }
            set
            {
                if (_appointment != value)
                {
                    _appointment = value;
                    OnPropertyChanged(() => Appointment);
                }
            }
        }
        #endregion

        #region Property Barcode
        private string _barcode;
        public string Barcode
        {
            get { return _barcode; }
            set
            {
                if (_barcode != value)
                {
                    _barcode = value;
                    OnPropertyChanged(() => Barcode);
                }
            }
        }
        #endregion

        #region Property BarcodeImagePath
        private string _barcodeImagePath;
        public string BarcodeImagePath
        {
            get { return _barcodeImagePath; }
            set
            {
                if (_barcodeImagePath != value)
                {
                    _barcodeImagePath = value;
                    OnPropertyChanged(() => BarcodeImagePath);
                }
            }
        }
        #endregion

        #endregion

        public AppointmentCardViewModel(Appointment appointment)
        {
            Appointment = appointment;

            //var employeeCode = WebContext.Current.User.Name.ToUpper();
            var employeeCode = appointment.LastLog.EmployeeCode;
            var lo = _dc.Load(_dc.GetEmployeeByEmployeeCodeQuery(employeeCode));
            lo.Completed += (s, args) =>
                {
                    Employee lo2 = lo.Entities.First();

                    EmployeeName = string.Format("{0}{1}  {2}", lo2.Title.Name, lo2.FirstName, lo2.LastName);
                    DivisionName = lo2.Division != null ? lo2.Division.ThaiName : "";
                };

            Barcode = string.Format("QSA{0:0000000}", Appointment.AppointmentID);
            BarcodeImagePath = string.Format(@"http://202.28.92.21/WebService/BarcodeService/BarQode/Appt/{0}", Appointment.AppointmentID);
        }
    }
}
